/**
 * 
 */
package com.example.android.moemoeru;

/**
 * @author mikan
 *
 */

/**
 * Imports
 */
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import java.util.Random;


public class dataBase  extends Activity {

	/**
	 * 
	 */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		  SQLiteDatabase moeDB = null;
		  String Data="";
		  
		  /* Create a Database. */
		  try {
			  moeDB = this.openOrCreateDatabase("moesentence", MODE_PRIVATE, null);
			  /* Create a tag in the Database. */
			  moeDB.execSQL("CREATE TABLE IF NOT EXISTS tag (id INTEGER UNIQUE, name VARCHAR, type INTEGER,)"); //character ids
			  /* Create a serie in the Database. */
			  moeDB.execSQL("CREATE TABLE IF NOT EXISTS serie (id INTEGER UNIQUE, name VARCHAR, syear INT, snumberchapters INT, favicon VARCHAR, stags VARCHAR,  characters VARCHAR)");
			  /* Create a character in the Database. */
			  moeDB.execSQL("CREATE TABLE IF NOT EXISTS character (id INTEGER UNIQUE, name VARCHAR, csurname VARCHAR, cyears INT, cgenere CHARACTER, cappears VARCHAR, seiyuu VARCHAR, ctags VARCHAR, series VARCHAR, sentences VARCHAR)");
			  /* Create a sentence in the Database. */
			  moeDB.execSQL("CREATE TABLE IF NOT EXISTS sentence (id INTEGER, langid INTEGER, name VARCHAR, chapter INTEGER, tags VARCHAR, character VARCHAR)");
			  /* Creating languate table in the Database */
			 
		      /* Insert data to a tag*/
			  //type = 0 serie, 1 character, 2 sentence
			  moeDB.execSQL("INSERT INTO tag (tid, name, type) VALUES (0, 'Tsundere', 1);");
			  moeDB.execSQL("INSERT INTO tag (tid, name, type) VALUES (1, 'Action', 0);");
			  moeDB.execSQL("INSERT INTO tag (tid, name, type) VALUES (2, 'moe', 2);");
			  moeDB.execSQL("INSERT INTO tag (tid, name, type) VALUES (3, 'moe', 1);");
			  moeDB.execSQL("INSERT INTO tag (tid, name, type) VALUES (3, 'moe', 0);");
					  //" VALUES ('Saranga', 22);");
			  /* Insert data to a serie*/
			  moeDB.execSQL("INSERT INTO serie (sid, name, syear, snumberchapters, favicon, stargs, characters) VALUES (0, 'Hayate no Gotoku!', 2007, 52, 'null', '1', '0');");
			  /* Insert data to a character*/
			  moeDB.execSQL("INSERT INTO character (cid, name, csurname, cyears, cgenere, cappears, seiyuu, ctags, series, sentences) VALUES (0, 'Nagi', 'Sanzenin', 13, 'F', '1-52', 'Kugimiya Rie', '0, 1', '0', '0');");
			  /* Insert data to a sentence*/
			  moeDB.execSQL("INSERT INTO sentence (id, name, chapter, tags, character) VALUES (0, 'Oi, Hayate!', '2', '0');");
		   
			  // TODO delete this part
			  /*retrieve data from database */
		   Cursor c = moeDB.rawQuery("SELECT * FROM sentence" , null);
		 
		   int Column1 = c.getColumnIndex("phrase");
		   int Column2 = c.getColumnIndex("chapter");
		 
		   // Check if our result was valid.
		   c.moveToFirst();
		   if (c != null) {
		    // Loop through all Results
		    do {
		     String Name = c.getString(Column1);
		     int Age = c.getInt(Column2);
		     Data =Data +Name+"/"+Age+"\n";
		    }while(c.moveToNext());
		   }
		   TextView tv = new TextView(this);
		   tv.setText(Data);
		   setContentView(tv);
		  }
		  catch(Exception e) {
		   Log.e("Error", "Error", e);
		  } finally {
		   if (moeDB != null)
		    moeDB.close();
		  }
	}
	
	@SuppressWarnings("null")
	public String[] getNames(String taula){
		SQLiteDatabase moeDB = null;
		String data[]=null;
		  /* Create a Database. */
		  try {
			  moeDB = openOrCreateDatabase("moesentence", MODE_PRIVATE, null);
			  Cursor c = moeDB.rawQuery("SELECT name FROM "+taula, null);
			  c.moveToFirst();
			  if (c != null){
				  int i=0;
				  do {
					  data[i] = c.getString(0);
					  i++;
				  } while (c.moveToNext());
			  }
		  } catch(Exception e) {
		   Log.e("Error", "Error", e);
		  } finally {
		   if (moeDB != null)
		    moeDB.close();
		  }
		return data;
	}
	
	public String getRandom() {
		String val="";
		SQLiteDatabase moeDB = null;
		Random a = new Random();
		try {
			  moeDB = openOrCreateDatabase("moesentence", MODE_PRIVATE, null);
			  Cursor c= null;
			  do {
				  c = moeDB.rawQuery("SELECT name FROM sentence WHERE id = "+a.nextInt(), null);
				  
			  } while (!c.moveToFirst());
			  val = c.getString(0); 
		  } catch(Exception e) {
		   Log.e("Error", "Error", e);
		  } finally {
		   if (moeDB != null)
		    moeDB.close();
		  }
		return val;
	}

}
